import axios from 'axios'
import { Message, Loading } from 'element-ui';
import {requestData} from '@/api'
import QS from 'qs';
import {CONFIG_DATA} from '../config'
/**
 * @Content_Type_status 0formData   1json   2文件上传
 */
axios.interceptors.request.use(
    config => {
        config.headers['Content-Type'] = CONFIG_DATA.Content_Type_status == 0?'application/x-www-form-urlencoded':CONFIG_DATA.Content_Type_status == 1?'application/json;charset=UTF-8':'multipart/form-data'
        return config
    },
    error => {
        return Promise.error(error)
    }
)
let judge = (status, msg) => {
    Message.error(msg)
    if(msg == "您未拥有此权限"){
        //执行自动登陆
        let loginData = sessionStorage.getItem("USER_AOTU");
        if(!loginData){
            //检测到权限异常
            sessionStorage.clear()
            location.reload();
            return Promise.reject(msg);
        }
        return POST("/login",JSON.parse(loginData),"/easyapi/doc/auth").catch(res => {
            sessionStorage.clear()
            location.reload();
            return Promise.reject(msg);
        })
        // return requestData(4, "/login", JSON.parse(loginData)).then(res => {
        //     return Promise.reject(msg);
        // }).catch(res => {
        //     //检测到权限异常
        //     sessionStorage.clear()
        //     location.reload();
        //     return Promise.reject(msg);
        // });
    }
    return Promise.reject(msg);

};
axios.interceptors.response.use(
    response => {
        let state = response.data.code || '';
        if (state === 200 || sessionStorage.mock === 'true') {
            return Promise.resolve(response)
        } else {
            state = response.data.errorCode
            let msg = response.data.errorMsg?response.data.errorMsg:response.data.remark
            judge(state, msg?msg:'系统异常');
        }
    },
    error => {
        Message.error('服务器异常')
        return Promise.resolve(error)
    }
)


const DEFAULT_BASE_URL = CONFIG_DATA.BASE_URL
/**
 * GET方式
 * @param {*} url 
 * @param {*} params 
 * @param {*} BASE_URL 
 */
export const GET = (url, params, BASE_URL = DEFAULT_BASE_URL) => {
    return new Promise((resolve, reject) => {
        axios.get(BASE_URL + url, { ...params }).then(res=> {
            // if(sessionStorage.mock) return resolve(res)
            resolve(res.data)
        }).catch(err => {
            reject(err.data)
        })
    })
}
/**
 * POST 方式
 * @param {*} url 
 * @param {*} params 
 * @param {*} BASE_URL 
 */

//请求过滤器 kele
function requestFilter(){

}
//响应过滤器 kele
function responseFilter(responseData){
    return  responseData;
}
//响应过滤器 kele
function errorFilter(errorData){
    return  errorData;
}
export const POST = (url, params, BASE_URL = DEFAULT_BASE_URL) => {
    if(CONFIG_DATA.Content_Type_status == 0){
        params = QS.stringify(params)
    }
    return new Promise((resolve, reject) => {
        axios.post(BASE_URL + url, params).then(res=> {
            resolve(responseFilter(res.data.data))
        }).catch(err => {
            reject(errorFilter(err))
        })
    })
}
